<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li class="active"><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 什么是session">1. 什么是session</a></li><li><a href="#t12. cookie与session区别">2. cookie与session区别</a></li><li><a href="#t23. session实现">3. session实现</a></li><li><a href="#t34. session中间件">4. session中间件</a></li><li><a href="#t45. 实现sesion计数器">5. 实现sesion计数器</a></li><li><a href="#t56. session实现权限">6. session实现权限</a></li><li><a href="#t67. 自定义存储位置">7. 自定义存储位置</a><ul><li><a href="#t77.1 保存到文件中">7.1 保存到文件中</a></li><li><a href="#t87.2 保存到Redis数据库中">7.2 保存到Redis数据库中</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x4EC0;&#x4E48;&#x662F;session">1. &#x4EC0;&#x4E48;&#x662F;session <a href="#t01. &#x4EC0;&#x4E48;&#x662F;session"> # </a></h2>
<ul>
<li>session&#x662F;&#x53E6;&#x4E00;&#x79CD;&#x8BB0;&#x5F55;&#x5BA2;&#x6237;&#x72B6;&#x6001;&#x7684;&#x673A;&#x5236;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x662F;Cookie&#x4FDD;&#x5B58;&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#xFF0C;&#x800C;session&#x4FDD;&#x5B58;&#x5728;&#x670D;&#x52A1;&#x5668;&#x4E0A;</li>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x6D4F;&#x89C8;&#x5668;&#x8BBF;&#x95EE;&#x670D;&#x52A1;&#x5668;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x628A;&#x5BA2;&#x6237;&#x7AEF;&#x4FE1;&#x606F;&#x4EE5;&#x67D0;&#x79CD;&#x5F62;&#x5F0F;&#x8BB0;&#x5F55;&#x5728;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;session&#x3002;&#x5BA2;&#x6237;&#x7AEF;&#x6D4F;&#x89C8;&#x5668;&#x518D;&#x6B21;&#x8BBF;&#x95EE;&#x65F6;&#x53EA;&#x9700;&#x8981;&#x4ECE;&#x8BE5;Session&#x4E2D;&#x67E5;&#x627E;&#x8BE5;&#x5BA2;&#x6237;&#x7684;&#x72B6;&#x6001;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;</li>
</ul>
<h2 id="t12. cookie&#x4E0E;session&#x533A;&#x522B;">2. cookie&#x4E0E;session&#x533A;&#x522B; <a href="#t12. cookie&#x4E0E;session&#x533A;&#x522B;"> # </a></h2>
<ol>
<li>cookie&#x6570;&#x636E;&#x5B58;&#x653E;&#x5728;&#x5BA2;&#x6237;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x4E0A;&#xFF0C;session&#x6570;&#x636E;&#x653E;&#x5728;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x3002;</li>
<li>cookie&#x4E0D;&#x662F;&#x5F88;&#x5B89;&#x5168;&#xFF0C;&#x522B;&#x4EBA;&#x53EF;&#x4EE5;&#x5206;&#x6790;&#x5B58;&#x653E;&#x5728;&#x672C;&#x5730;&#x7684;COOKIE&#x5E76;&#x8FDB;&#x884C;COOKIE&#x6B3A;&#x9A97; &#x8003;&#x8651;&#x5230;&#x5B89;&#x5168;&#x5E94;&#x5F53;&#x4F7F;&#x7528;session</li>
<li>session&#x4F1A;&#x5728;&#x4E00;&#x5B9A;&#x65F6;&#x95F4;&#x5185;&#x4FDD;&#x5B58;&#x5728;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x3002;&#x5F53;&#x8BBF;&#x95EE;&#x589E;&#x591A;&#xFF0C;&#x4F1A;&#x6BD4;&#x8F83;&#x5360;&#x7528;&#x4F60;&#x670D;&#x52A1;&#x5668;&#x7684;&#x6027;&#x80FD; &#x8003;&#x8651;&#x5230;&#x51CF;&#x8F7B;&#x670D;&#x52A1;&#x5668;&#x6027;&#x80FD;&#x65B9;&#x9762;,&#x5E94;&#x5F53;&#x4F7F;&#x7528;COOKIE</li>
<li>&#x5355;&#x4E2A;cookie&#x4FDD;&#x5B58;&#x7684;&#x6570;&#x636E;&#x4E0D;&#x80FD;&#x8D85;&#x8FC7;4K&#xFF0C;&#x5F88;&#x591A;&#x6D4F;&#x89C8;&#x5668;&#x90FD;&#x9650;&#x5236;&#x4E00;&#x4E2A;&#x7AD9;&#x70B9;&#x6700;&#x591A;&#x4FDD;&#x5B58;20&#x4E2A;cookie</li>
</ol>
<blockquote>
<p>&#x5C06;&#x767B;&#x9646;&#x4FE1;&#x606F;&#x7B49;&#x91CD;&#x8981;&#x4FE1;&#x606F;&#x5B58;&#x653E;&#x4E3A;session&#x3001;&#x5176;&#x4ED6;&#x4FE1;&#x606F;&#x5982;&#x679C;&#x9700;&#x8981;&#x4FDD;&#x7559;&#xFF0C;&#x53EF;&#x4EE5;&#x653E;&#x5728;cookie&#x4E2D;</p>
</blockquote>
<h2 id="t23. session&#x5B9E;&#x73B0;">3. session&#x5B9E;&#x73B0; <a href="#t23. session&#x5B9E;&#x73B0;"> # </a></h2>
<ol>
<li>&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x751F;&#x6210;&#x5168;&#x5C40;&#x552F;&#x4E00;&#x6807;&#x8BC6;&#x7B26;<code>session_id</code></li>
<li>&#x5728;&#x670D;&#x52A1;&#x5668;&#x5185;&#x5B58;&#x91CC;&#x5F00;&#x8F9F;&#x6B64;<code>session_id</code>&#x5BF9;&#x5E94;&#x7684;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;</li>
<li>&#x5C06;<code>session_id</code>&#x4F5C;&#x4E3A;&#x5168;&#x5C40;&#x552F;&#x4E00;&#x6807;&#x793A;&#x7B26;&#x901A;&#x8FC7;<code>cookie</code>&#x53D1;&#x9001;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;</li>
<li>&#x4EE5;&#x540E;&#x5BA2;&#x6237;&#x7AEF;&#x518D;&#x6B21;&#x8BBF;&#x95EE;&#x670D;&#x52A1;&#x5668;&#x65F6;&#x4F1A;&#x628A;<code>session_id</code>&#x901A;&#x8FC7;&#x8BF7;&#x6C42;&#x5934;&#x4E2D;&#x7684;<code>cookie</code>&#x53D1;&#x9001;&#x7ED9;&#x670D;&#x52A1;&#x5668;</li>
<li>&#x670D;&#x52A1;&#x5668;&#x518D;&#x901A;&#x8FC7;<code>session_id</code>&#x628A;&#x6B64;&#x6807;&#x8BC6;&#x7B26;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7684;&#x6570;&#x636E;&#x53D6;&#x51FA;</li>
</ol>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> cookieParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cookie-parser&apos;</span>);
<span class="hljs-keyword">var</span> app = express();
app.use(cookieParser());
<span class="hljs-comment">//&#x5B58;&#x653E;&#x4F1A;&#x8BDD;&#x6570;&#x636E; key&#x5361;&#x53F7; value&#x5C31;&#x662F;&#x5361;&#x53F7;&#x5BF9;&#x5E94;&#x7684;&#x6570;&#x636E;&#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">var</span> sessions = {};
<span class="hljs-comment">//&#x4E0E;&#x5BA2;&#x6237;&#x7AEF;&#x7EA6;&#x5B9A;&#x7684;&#x4F1A;&#x8BDD;ID</span>
<span class="hljs-keyword">var</span> SESSION_KEY = <span class="hljs-string">&apos;connect.sid&apos;</span>
<span class="hljs-comment">//&#x5F53;&#x7528;&#x6237;&#x8BBF;&#x95EE;&#x6839;&#x76EE;&#x5F55;&#x7684;&#x65F6;&#x5019; &#x6267;&#x884C;&#x5BF9;&#x5E94;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.setHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;text/html;charset=utf-8&apos;</span>);
   <span class="hljs-comment">// 1. &#x5148;&#x53D6;&#x51FA;cookie&#x4E2D;&#x7684;sessionId &#x5361;&#x53F7;</span>
    <span class="hljs-keyword">var</span> sessionId = req.cookies[SESSION_KEY];
    <span class="hljs-comment">// &#x5982;&#x679C;&#x6709;&#x5361;&#x53F7;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x6709;ID&#x7684;&#x8BDD; &#x8001;&#x987E;&#x5BA2;</span>
    <span class="hljs-keyword">if</span>(sessionId){
        <span class="hljs-comment">//&#x53D6;&#x51FA;&#x6B64;&#x5361;&#x53F7;&#x5BF9;&#x5E94;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x4F59;&#x989D;</span>
        <span class="hljs-keyword">var</span> sessionObj = sessions[sessionId];
        <span class="hljs-keyword">if</span>(sessionObj){
            <span class="hljs-comment">//&#x6263;&#x6389;10&#x5757;&#x94B1;</span>
            sessionObj.balance = sessionObj.balance <span class="hljs-number">-10</span>;
            res.send(<span class="hljs-string">&apos;&#x6B22;&#x8FCE;&#x4F60;&#x8001;&#x987E;&#x5BA2;&#xFF0C;&#x4F60;&#x5361;&#x4E0A;&#x8FD8;&#x5269;&apos;</span>+sessionObj.balance);
        }<span class="hljs-keyword">else</span>{
            genId(res);
        }
    <span class="hljs-comment">//&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7684;&#x8BDD;&#x5C31;&#x662F;&#x65B0;&#x987E;&#x5BA2;</span>
    }<span class="hljs-keyword">else</span>{
        genId(res);
    }
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">genId</span>(<span class="hljs-params">res</span>)</span>{
        <span class="hljs-comment">//&#x7531;&#x5E97;&#x5BB6;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x552F;&#x4E00;&#x7684;&#x5361;&#x53F7;</span>
        <span class="hljs-keyword">var</span> id = <span class="hljs-built_in">Date</span>.now()+<span class="hljs-string">&apos;&apos;</span>+<span class="hljs-built_in">Math</span>.random();
        <span class="hljs-comment">//&#x8981;&#x5728;&#x5E97;&#x5BB6;&#x7684;&#x5C0F;&#x672C;&#x4E0A;&#x8BB0;&#x5F55;&#x4E00;&#x4E0B;&#x6B64;&#x5361;&#x53F7;&#x5BF9;&#x5E94;&#x7684;&#x4F59;&#x989D;</span>
        sessions[id] = {<span class="hljs-attr">balance</span>:<span class="hljs-number">100</span>};
        <span class="hljs-comment">//&#x628A;&#x8FD9;&#x4E2A;&#x5361;&#x53D1;&#x7ED9;&#x987E;&#x5BA2;&#x5E26;&#x56DE;&#x5BB6;</span>
        res.cookie(SESSION_KEY,id);
        <span class="hljs-comment">//&#x544A;&#x8BC9; &#x7528;&#x6237;&#x9001;&#x4ED6;&#x4E00;&#x5F20;&#x5361;</span>
        res.send(<span class="hljs-string">&apos;&#x6B22;&#x8FCE;&#x4F60;&#x65B0;&#x987E;&#x5BA2;&#xFF0C;&#x9001;&#x4F60;&#x4E00;&#x5F20;&#x4EF7;&#x503C;100&#x5143;&#x7684;&#x526A;&#x53D1;&#x5361;&apos;</span>);
    }
});

app.listen(<span class="hljs-number">9090</span>);
</code></pre>
<h2 id="t34. session&#x4E2D;&#x95F4;&#x4EF6;">4. session&#x4E2D;&#x95F4;&#x4EF6; <a href="#t34. session&#x4E2D;&#x95F4;&#x4EF6;"> # </a></h2>
<p><a href="https://github.com/expressjs/session">session</a></p>
<p><img src="http://img.zhufengpeixun.cn/session.png" alt="session"></p>
<pre><code class="lang-javascript">$ npm install express-session
</code></pre>
<table>
<thead>
<tr>
<th style="text-align:left">&#x53C2;&#x6570;</th>
<th style="text-align:left">&#x63CF;&#x8FF0;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">name</td>
<td style="text-align:left">&#x8BBE;&#x7F6E; cookie &#x4E2D;&#xFF0C;&#x4FDD;&#x5B58; session &#x7684;&#x5B57;&#x6BB5;&#x540D;&#x79F0;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; connect.sid</td>
</tr>
<tr>
<td style="text-align:left">store</td>
<td style="text-align:left">session &#x7684;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;&#xFF0C;&#x9ED8;&#x8BA4;&#x5B58;&#x653E;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; redis&#xFF0C;mongodb &#x7B49;</td>
</tr>
<tr>
<td style="text-align:left">secret</td>
<td style="text-align:left">&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;&#x7684; secret &#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6765;&#x8BA1;&#x7B97; hash &#x503C;&#x5E76;&#x653E;&#x5728; cookie &#x4E2D;&#xFF0C;&#x4F7F;&#x4EA7;&#x751F;&#x7684; signedCookie &#x9632;&#x7BE1;&#x6539;</td>
</tr>
<tr>
<td style="text-align:left">cookie</td>
<td style="text-align:left">&#x8BBE;&#x7F6E;&#x5B58;&#x653E; session id &#x7684; cookie &#x7684;&#x76F8;&#x5173;&#x9009;&#x9879;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; (default: { path: &apos;/&apos;, httpOnly: true, secure: false, maxAge: null })</td>
</tr>
<tr>
<td style="text-align:left">genid</td>
<td style="text-align:left">&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x65B0;&#x7684; session_id &#x65F6;&#xFF0C;&#x6240;&#x4F7F;&#x7528;&#x7684;&#x51FD;&#x6570;&#xFF0C; &#x9ED8;&#x8BA4;&#x4F7F;&#x7528; uid2 &#x8FD9;&#x4E2A; npm &#x5305;</td>
</tr>
<tr>
<td style="text-align:left">rolling</td>
<td style="text-align:left">&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x90FD;&#x91CD;&#x65B0;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A; cookie&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; false</td>
</tr>
<tr>
<td style="text-align:left">saveUninitialized</td>
<td style="text-align:left">&#x662F;&#x6307;&#x65E0;&#x8BBA;&#x6709;&#x6CA1;&#x6709;session cookie&#xFF0C;&#x6BCF;&#x6B21;&#x8BF7;&#x6C42;&#x90FD;&#x8BBE;&#x7F6E;&#x4E2A;session cookie &#xFF0C;&#x9ED8;&#x8BA4;&#x7ED9;&#x4E2A;&#x6807;&#x793A;&#x4E3A; connect.sid</td>
</tr>
<tr>
<td style="text-align:left">resave</td>
<td style="text-align:left">&#x662F;&#x6307;&#x6BCF;&#x6B21;&#x8BF7;&#x6C42;&#x90FD;&#x91CD;&#x65B0;&#x8BBE;&#x7F6E;session cookie&#xFF0C;&#x5047;&#x8BBE;&#x4F60;&#x7684;cookie&#x662F;10&#x5206;&#x949F;&#x8FC7;&#x671F;&#xFF0C;&#x6BCF;&#x6B21;&#x8BF7;&#x6C42;&#x90FD;&#x4F1A;&#x518D;&#x8BBE;&#x7F6E;10&#x5206;&#x949F;</td>
</tr>
</tbody>
</table>
<h2 id="t45. &#x5B9E;&#x73B0;sesion&#x8BA1;&#x6570;&#x5668;">5. &#x5B9E;&#x73B0;sesion&#x8BA1;&#x6570;&#x5668; <a href="#t45. &#x5B9E;&#x73B0;sesion&#x8BA1;&#x6570;&#x5668;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">let</span> session = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express-session&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> FileStore = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./filestore&apos;</span>)(session);
<span class="hljs-keyword">let</span> app = express();
app.use(session({
    <span class="hljs-attr">secret</span>: <span class="hljs-string">&apos;zfpx&apos;</span>,
    <span class="hljs-attr">resave</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">saveUninitialized</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">store</span>: <span class="hljs-keyword">new</span> FileStore({
        <span class="hljs-attr">root</span>: path.join(__dirname, <span class="hljs-string">&apos;sessions&apos;</span>),
        <span class="hljs-attr">maxAge</span>: <span class="hljs-number">1000</span>,
        <span class="hljs-attr">gc</span>: <span class="hljs-number">1</span>
    })
}));
app.get(<span class="hljs-string">&apos;/visit&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    <span class="hljs-keyword">let</span> visit = req.session.visit;
    <span class="hljs-keyword">if</span> (visit) {
        visit = visit + <span class="hljs-number">1</span>;
    } <span class="hljs-keyword">else</span> {
        visit = <span class="hljs-number">1</span>;
    }
    req.session.visit = visit;
    res.send(<span class="hljs-string">`&#x6B22;&#x8FCE;&#x4F60;&#x7684;&#x7B2C;<span class="hljs-subst">${visit}</span>&#x6B21;&#x5149;&#x4E34;`</span>);
});
app.listen(<span class="hljs-number">8080</span>);
</code></pre>
<h2 id="t56. session&#x5B9E;&#x73B0;&#x6743;&#x9650;">6. session&#x5B9E;&#x73B0;&#x6743;&#x9650; <a href="#t56. session&#x5B9E;&#x73B0;&#x6743;&#x9650;"> # </a></h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">var</span> cookieParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cookie-parser&apos;</span>);
<span class="hljs-keyword">var</span> session = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express-session&apos;</span>);
<span class="hljs-keyword">var</span> uuid = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;uuid&apos;</span>);
<span class="hljs-keyword">var</span> app = express();
app.set(<span class="hljs-string">&apos;view engine&apos;</span>, <span class="hljs-string">&apos;html&apos;</span>);
app.engine(<span class="hljs-string">&apos;html&apos;</span>, <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;ejs&apos;</span>).__express);
app.set(<span class="hljs-string">&apos;views&apos;</span>, __dirname);
app.use(<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;cookie-parser&apos;</span>)());

app.use(session({<span class="hljs-attr">secret</span>: <span class="hljs-string">&apos;zfpx&apos;</span>,
    <span class="hljs-attr">resave</span>: <span class="hljs-literal">true</span>,
    <span class="hljs-attr">saveUninitialized</span>: <span class="hljs-literal">true</span>}));

<span class="hljs-comment">/**
 * curl -v -H &quot;cookie: username=customer&quot; http://localhost:8080/user
 */</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkUser</span>(<span class="hljs-params">req, res, next</span>) </span>{
    <span class="hljs-keyword">if</span> (req.session &amp;&amp; req.session.username)
        next();
    <span class="hljs-keyword">else</span>
        res.redirect(<span class="hljs-string">&apos;/&apos;</span>);
}

<span class="hljs-comment">//&#x8FDB;&#x5165;&#x767B;&#x5F55;&#x9875;</span>
app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    res.render(<span class="hljs-string">&apos;index&apos;</span>);
});

<span class="hljs-comment">//&#x767B;&#x5F55;</span>
app.get(<span class="hljs-string">&apos;/login&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    req.session.username = req.query.username;
    res.redirect(<span class="hljs-string">&apos;/user&apos;</span>);
});

<span class="hljs-comment">//&#x7528;&#x6237;&#x9875;&#x9762;</span>
app.get(<span class="hljs-string">&apos;/user&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    <span class="hljs-built_in">console</span>.log(req.session);
    res.render(<span class="hljs-string">&apos;user&apos;</span>, {<span class="hljs-attr">username</span>: req.session.username});
});

<span class="hljs-comment">//&#x7528;&#x6237;&#x9000;&#x51FA;</span>
app.get(<span class="hljs-string">&apos;/logout&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    req.session.usrename = <span class="hljs-literal">null</span>;
    res.redirect(<span class="hljs-string">&apos;/&apos;</span>);
});

app.listen(<span class="hljs-number">8080</span>);
</code></pre>
<h2 id="t67. &#x81EA;&#x5B9A;&#x4E49;&#x5B58;&#x50A8;&#x4F4D;&#x7F6E;">7. &#x81EA;&#x5B9A;&#x4E49;&#x5B58;&#x50A8;&#x4F4D;&#x7F6E; <a href="#t67. &#x81EA;&#x5B9A;&#x4E49;&#x5B58;&#x50A8;&#x4F4D;&#x7F6E;"> # </a></h2>
<table>
<thead>
<tr>
<th style="text-align:left">&#x65B9;&#x6CD5;</th>
<th style="text-align:left">&#x542B;&#x4E49;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">get</td>
<td style="text-align:left">&#x83B7;&#x53D6;sessoin</td>
</tr>
<tr>
<td style="text-align:left">set</td>
<td style="text-align:left">&#x8BBE;&#x7F6E;session</td>
</tr>
<tr>
<td style="text-align:left">destroy</td>
<td style="text-align:left">&#x9500;&#x6BC1;session</td>
</tr>
</tbody>
</table>
<h3 id="t77.1 &#x4FDD;&#x5B58;&#x5230;&#x6587;&#x4EF6;&#x4E2D;">7.1 &#x4FDD;&#x5B58;&#x5230;&#x6587;&#x4EF6;&#x4E2D; <a href="#t77.1 &#x4FDD;&#x5B58;&#x5230;&#x6587;&#x4EF6;&#x4E2D;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;util&apos;</span>);
<span class="hljs-keyword">let</span> mkdirp = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;mkdirp&apos;</span>);
<span class="hljs-keyword">let</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createFileStore</span>(<span class="hljs-params">session</span>) </span>{
    <span class="hljs-keyword">const</span> Store = session.Store;
    util.inherits(FileStore, Store);
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">FileStore</span>(<span class="hljs-params">options</span>) </span>{
        <span class="hljs-keyword">let</span> { dir = path.resolve(__dirname, <span class="hljs-string">&apos;sessions&apos;</span>) } = options || {};
        <span class="hljs-keyword">this</span>.dir = dir;
        mkdirp(<span class="hljs-keyword">this</span>.dir);
    }
    FileStore.prototype.resolve = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">sessionId</span>) </span>{
        <span class="hljs-keyword">return</span> path.join(<span class="hljs-keyword">this</span>.dir, <span class="hljs-string">`<span class="hljs-subst">${sessionId}</span>.json`</span>);
    }
    FileStore.prototype.get = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">sessionId, callback</span>) </span>{
        fs.readFile(<span class="hljs-keyword">this</span>.resolve(sessionId), <span class="hljs-string">&apos;utf8&apos;</span>, (err, data) =&gt; {
            <span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">return</span> callback(err);
            callback(err, <span class="hljs-built_in">JSON</span>.parse(data));
        });
    }

    FileStore.prototype.set = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">sessionId, session, callback</span>) </span>{
        fs.writeFile(<span class="hljs-keyword">this</span>.resolve(sessionId), <span class="hljs-built_in">JSON</span>.stringify(session), callback);
    }

    FileStore.prototype.destroy = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">sessionId, callback</span>) </span>{
        fs.unlink(<span class="hljs-keyword">this</span>.resolve(sessionId), callback);
    }
    <span class="hljs-keyword">return</span> FileStore;
}

<span class="hljs-built_in">module</span>.exports = createFileStore;
<span class="hljs-comment">//FileStore</span>

</code></pre>
<h3 id="t87.2 &#x4FDD;&#x5B58;&#x5230;Redis&#x6570;&#x636E;&#x5E93;&#x4E2D;">7.2 &#x4FDD;&#x5B58;&#x5230;Redis&#x6570;&#x636E;&#x5E93;&#x4E2D; <a href="#t87.2 &#x4FDD;&#x5B58;&#x5230;Redis&#x6570;&#x636E;&#x5E93;&#x4E2D;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;util&apos;</span>);
<span class="hljs-keyword">var</span> redis = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;redis&quot;</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createRedisStore</span>(<span class="hljs-params">session</span>) </span>{
    <span class="hljs-keyword">const</span> Store = session.Store;
    util.inherits(RedisStore, Store);
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">RedisStore</span>(<span class="hljs-params">options = {}</span>) </span>{
        <span class="hljs-keyword">this</span>.client = redis.createClient(options.port || <span class="hljs-number">6379</span>, options.host || <span class="hljs-string">&apos;localhost&apos;</span>);
    }
    RedisStore.prototype.get = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">sessionId, callback</span>) </span>{
        <span class="hljs-keyword">this</span>.client.get(<span class="hljs-built_in">String</span>(sessionId), (err, data) =&gt; {
            callback(err, <span class="hljs-built_in">JSON</span>.parse(data));
        });
    }

    RedisStore.prototype.set = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">sessionId, session, callback</span>) </span>{
        <span class="hljs-keyword">this</span>.client.set(sessionId, <span class="hljs-built_in">JSON</span>.stringify(session), callback);
    }

    RedisStore.prototype.destroy = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">sessionId, callback</span>) </span>{
        <span class="hljs-keyword">this</span>.client.del(sessionId, callback);
    }
    <span class="hljs-keyword">return</span> RedisStore;
}

<span class="hljs-built_in">module</span>.exports = createRedisStore;
<span class="hljs-comment">//FileStore</span>
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
